/**
 * Created with IntelliJ IDEA.
 * Description: 向上转型介绍
 * User: 阿宾
 * Date: 2024-05-19
 * Time: 18:43
 */
class Animal {
    public String name;
    public int age;

    //构造方法
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public void eat(){
        System.out.println(this.name + "在吃东西");
    }
}
//子类1
class Dog extends Animal {
    //构造方法
    public Dog(String name,int age) {
        super(name,age);
    }
    public void wangWang() {
        System.out.println(this.name+" 正在旺旺！");
    }
    public void eat(){
        System.out.println(this.name + "在吃狗粮");
    }
}
//子类2
class Bird extends Animal {
    //构造方法
    public Bird(String name, int age) {
        super(name, age);
    }
    public void fly() {
        System.out.println(this.name+" 正在飞！");
    }
}

public class Test02 {
    public static void func1(Animal animal){

    }
    public static Animal func2(){
        return new Bird("小鸟",9);//因为Animal是Bird的父类，所以可以作为返回类型
    }


    public static void main(String[] args) {


        //向上转型
        Animal animal = new Dog("狗",15);
        if(animal instanceof  Bird b){
            b.fly();
        }else if(animal instanceof  Dog d){
            d.eat();
        }else {
            System.out.println("执行错误");
        }
        /*if(animal instanceof Bird){
            //向下转型
            Bird bird = (Bird) animal;
            bird.fly();
        }else {
            System.out.println("执行错误");
        }*/

    }

    public static void main1(String[] args) {
        //向上转型，即父类引用指向子类对象
        Animal a1 = new Dog("狗",18);
        //调用方法eat()，该方法在父类和子类Dog中都存在，构成方法的重写
        a1.eat();
    }
}
